package com.lancoo.cvas.controller;

import com.lancoo.cvas.aspect.LogAnnotation;
import com.lancoo.cvas.entity.TeachingClassStudent;
import com.lancoo.cvas.pojo.dto.HourTypeDto;
import com.lancoo.cvas.pojo.vo.*;
import com.lancoo.cvas.service.AssessmentMethodService;
import com.lancoo.cvas.service.ParameterSettingService;
import com.lancoo.cvas.service.TeachingClassService;
import com.lancoo.cvas.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Api(tags = "教学班")
@RestController
@RequestMapping("/api/v5.0.1/web/teachingClass")
public class TeachingClassController {


    @Resource
    private TeachingClassService teachingClassService;

    @Resource
    private AssessmentMethodService assessmentMethodService;
    @Resource
    private ParameterSettingService parameterSettingService;


    @GetMapping("/addTeachingClass")
    @ApiOperation("根据taskId生成行政班对应的教学班")
    @LogAnnotation(operationType = "2", operationContent = "根据taskId生成行政班对应的教学班")
    public Result<String> addTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                           @ApiParam(value = "是否修改教学班名称", required = true) @RequestParam Boolean isUpdate) {
        return Result.ofSuccess(teachingClassService.addBatchTeachingClass(taskId, isUpdate));
    }


    @GetMapping("/addParameterSetting")
    @ApiOperation("生成课程环节参数表")
    @LogAnnotation(operationType = "2", operationContent = "生成课程环节参数表")
    public Result<String> addParameterSetting(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(teachingClassService.addParameterSetting(taskId));
    }


    @GetMapping("/getTeachingClassList")
    @ApiOperation("获取全部生成的教学班")
    @LogAnnotation(operationType = "3", operationContent = "获取全部生成的教学班")
    public Result<TeachingClassDtoVo> getTeachingClassList(@ApiParam(value = "任务流程id", required = true) @RequestHeader(required = true) Integer taskId,
                                                           @ApiParam(value = "年级id传null查全部", required = false) @RequestParam(required = false) String gradeId,
                                                           @ApiParam(value = "专业id传null查全部", required = false) @RequestParam(required = false) String majorId,
                                                           @ApiParam(value = "类型id传null查全部", required = false) @RequestParam(required = false) Integer courseType,
                                                           @ApiParam(value = "课程/环节关键字", required = false) @RequestParam(required = false) String keyword) {
        return Result.ofSuccess(teachingClassService.getTeachingClassList(taskId, gradeId, majorId, courseType, keyword));
    }


    @GetMapping("/getTeachingClassListV2")
    @ApiOperation("获取全部生成的教学班V2")
    @LogAnnotation(operationType = "3", operationContent = "获取全部生成的教学班")
    public Result<TeachingClassDtoVo> getTeachingClassListV2(@ApiParam(value = "任务流程id", required = true) @RequestHeader(required = true) Integer taskId,
                                                             @ApiParam(value = "年级id传null查全部", required = false) @RequestParam(required = false) String gradeId,
                                                             @ApiParam(value = "类型id传null查全部", required = false) @RequestParam(required = false) Integer courseType,
                                                             @ApiParam(value = "0未设置 1以设置传null查全部", required = false) @RequestParam(required = false) Integer state,
                                                             @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                             @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize,
                                                             @ApiParam(value = "课程/环节关键字", required = false) @RequestParam(required = false) String keyword) {
        return Result.ofSuccess(teachingClassService.getTeachingClassListV2(taskId, gradeId, courseType, keyword, currentPage, pageSize, state));
    }

    @ApiOperation("获取下次设置单双周值")
    @GetMapping("/getCourseNextOddEven")
    @LogAnnotation(operationType = "3", operationContent = "获取下次设置单双周值")
    public Result<Integer> getNextOddEven(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(teachingClassService.oddEvenWeekStatistics(taskId));
    }


    @PostMapping("/getClassInfoByClassIds")
    @ApiOperation("根据行政班信id获取行政班名称")
    @LogAnnotation(operationType = "3", operationContent = "根据行政班信id获取行政班名称")
    public Result<List<ClassInfoVoDto>> getClassInfoByClassIds(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                               @ApiParam(value = "年级id传null查全部", required = true) @RequestBody List<Long> classIds) {
        return Result.ofSuccess(teachingClassService.getClassInfoByClassIds(taskId, classIds));
    }


    @GetMapping("/updateTeachingClassType")
    @ApiOperation("修改是否需要选课")
    @LogAnnotation(operationType = "2", operationContent = "修改是否需要选课")
    public Result<Integer> updateTeachingClassType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                   @ApiParam(value = "年级Id", required = true) @RequestParam String gradeId,
                                                   @ApiParam(value = "课程Id", required = true) @RequestParam Integer courseId,
                                                   @ApiParam(value = "类型id 1课程 2 环节", required = false) @RequestParam Integer courseType,
                                                   @ApiParam(value = "是否需要选课，不需要传0，需要传1", required = true) @RequestParam Integer teachingClassType) {
        return Result.ofSuccess(teachingClassService.updateTeachingClassType(taskId, gradeId, courseId, courseType, teachingClassType));
    }


    @PostMapping("/modifyTeachingClassNum")
    @ApiOperation("修改教学班数量")
    @LogAnnotation(operationType = "2", operationContent = "修改选课教学班数量")
    public Result<String> modifyTeachingClassNum(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                 @ApiParam(value = "任务流程id", required = true) @RequestBody ModifyTeachingClassNumDto modifyTeachingClassNumDto) {

        String res = "";
        synchronized (this) {
            res = teachingClassService.modifyTeachingClassNum(taskId, modifyTeachingClassNumDto);
        }
        return Result.ofSuccess(res);
    }


    @GetMapping("/checkSubClass")
    @ApiOperation("检查该课程如果是选课是否有拆班")
    @LogAnnotation(operationType = "3", operationContent = "检查该是选课课程是否有拆班教学班")
    public Result<Boolean> checkSubClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                         @ApiParam(value = "任务流程id", required = true) @RequestParam String gradeId,
                                         @ApiParam(value = "任务流程id", required = true) @RequestParam Long courseId) {

        return Result.ofSuccess(teachingClassService.checkSubClass(taskId, gradeId, courseId));
    }




    @PostMapping("/recoverSelection")
    @ApiOperation("恢复选课")
    @LogAnnotation(operationType = "2", operationContent = "选课数据恢复")
    public Result<String> recoverSelection(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                           @ApiParam(value = "恢复选课入参", required = true) @RequestBody RecoverSelectionDto recoverSelectionDto) {
        return Result.ofSuccess(teachingClassService.recoverSelection(taskId, recoverSelectionDto));
    }


    @GetMapping("/modifyAssessment")
    @ApiOperation("修改考核方式")
    @LogAnnotation(operationType = "2", operationContent = "修改考核方式")
    public Result<String> modifyAssessment(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                           @ApiParam(value = "课程/环节关键字", required = true) @RequestParam String gradeId,
                                           @ApiParam(value = "课程/环节关键字", required = true) @RequestParam Long courseId,
                                           @ApiParam(value = "课程/环节关键字", required = true) @RequestParam Long assessmentId) {
        return Result.ofSuccess(teachingClassService.modifyAssessment(taskId, gradeId, courseId, assessmentId));
    }


    @PostMapping("/addHourType")
    @ApiOperation("增加授课方式")
    @LogAnnotation(operationType = "2", operationContent = "增加授课方式")
    public Result<String> addHourType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                      @ApiParam(value = "前端传入授课方式相关参数", required = true) @RequestBody HourTypeInput hourTypeInput) {
        return Result.ofSuccess(teachingClassService.addHourType(taskId, hourTypeInput));
    }


    @PostMapping("/addHourTypeByTeachingClassIds")
    @ApiOperation("增加授课方式通过教学班id")
    @LogAnnotation(operationType = "2", operationContent = "通过教学班id增加授课方式")
    public Result<String> addHourTypeByTeachingClassIds(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                        @ApiParam(value = "前端传入授课方式相关参数", required = true) @RequestBody HourTypeDto hourTypeDto) {
        return Result.ofSuccess(teachingClassService.addHourTypeByTeachingClassIds(taskId, hourTypeDto));
    }


    @PostMapping("/deletedHourType")
    @ApiOperation("删除授课方式")
    @LogAnnotation(operationType = "4", operationContent = "通过教学班id增加授课方式")
    public Result<String> deletedHourType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                          @ApiParam(value = "前端传入授课方式相关参数", required = true) @RequestBody HourTypeInput hourTypeInput) {
        return Result.ofSuccess(teachingClassService.deletedHourType(taskId, hourTypeInput));
    }


    @PostMapping("/deletedHourTypeByTeachingClassIds")
    @ApiOperation("删除授课方式通过教学班id")
    @LogAnnotation(operationType = "4", operationContent = "通过教学班id增加授课方式")
    public Result<String> deletedHourTypeByTeachingClassIds(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                            @ApiParam(value = "前端传入授课方式相关参数", required = true) @RequestBody HourTypeDto hourTypeDto) {
        return Result.ofSuccess(teachingClassService.deletedHourTypeByTeachingClassIds(taskId, hourTypeDto));
    }

    @PostMapping("/assignTeachers")
    @ApiOperation("分配设置教师")
    @LogAnnotation(operationType = "2", operationContent = "分配教师")
    public Result<Boolean> assignTeachers(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                          @ApiParam(value = "分配教师传参", required = true) @RequestBody AssignTeachersDto assignTeachersDto) {
        return Result.ofSuccess(teachingClassService.assignTeachers(taskId, assignTeachersDto));
    }


    @GetMapping("/modifyRoomType")
    @ApiOperation("设置场地类型")
    @LogAnnotation(operationType = "2", operationContent = "设置场地类型")
    public Result<String> modifyRoomType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                         @ApiParam(value = "年级id", required = true) @RequestParam String gradeId,
                                         @ApiParam(value = "课程Id", required = true) @RequestParam Long courseId,
                                         @ApiParam(value = "授课方式", required = true) @RequestParam Integer hourType,
                                         @ApiParam(value = "场地类型", required = true) @RequestParam Integer roomType) {
        return Result.ofSuccess(teachingClassService.modifyRoomType(taskId, gradeId, courseId, hourType, roomType));
    }


    @GetMapping("/modifyTeachingClassInfo")
    @ApiOperation("修改教学班相关参数")
    @LogAnnotation(operationType = "2", operationContent = "修改教学班相关参数")
    public Result modifyTeachingClassInfo(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                          @ApiParam(value = "年级id", required = true) @RequestParam String gradeId,
                                          @ApiParam(value = "课程Id", required = true) @RequestParam Long courseId,
                                          @ApiParam(value = "授课方式 1 讲授 2 实验 3 上机 4 其他", required = true) @RequestParam(required = false) Integer hourType,
                                          @ApiParam(value = "1 课程 2 环节", required = true) @RequestParam Integer flag,
                                          @ApiParam(value = "0 未设置单双周 1设置单周 2设置双周", required = true) @RequestParam Integer sign,
                                          @ApiParam(value = "周次", required = true) @RequestParam String week,
                                          @ApiParam(value = "开始周次", required = true) @RequestParam Integer startWeek,
                                          @ApiParam(value = "结束周次", required = true) @RequestParam Integer endWeek,
                                          @ApiParam(value = "总学时", required = true) @RequestParam(required = false) Integer totalWeekNum,
                                          @ApiParam(value = "连上节次", required = true) @RequestParam(required = false) Integer connectSection,
                                          @ApiParam(value = "连上次数", required = true) @RequestParam(required = false) Integer connectNumber) {
        return teachingClassService.modifyTeachingClassInfo(taskId, gradeId, courseId, flag, hourType, week, totalWeekNum, connectSection, connectNumber, sign, startWeek, endWeek);
    }

    @GetMapping("/modifyIsEnable")
    @ApiOperation("设置是否可用")
    @LogAnnotation(operationType = "2", operationContent = "设置是否可用")
    public Result<String> modifyIsEnable(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                         @ApiParam(value = "年级id", required = true) @RequestParam String gradeId,
                                         @ApiParam(value = "课程Id", required = true) @RequestParam Long courseId,
                                         @ApiParam(value = "0 课程 1 环节", required = true) @RequestParam Integer flag,
                                         @ApiParam(value = "flag 0 已启用 1 未启用", required = true) @RequestParam Integer isEnable) {
        return Result.ofSuccess(parameterSettingService.modifyIsEnable(taskId, gradeId, courseId, flag, isEnable));
    }

    @ApiOperation("批量修改学生")
    @PostMapping("/updateStudent")
    @LogAnnotation(operationType = "2", operationContent = "批量修改教学班学生")
    public Result<List<TeachingClassStudent>> updateStudent(@ApiParam(value = "教学计划id", required = true) @RequestParam Long teachingClassId,
                                                            @ApiParam(value = "学生id", required = true) @RequestBody List<Long> studentIds) {
        return Result.ofSuccess(teachingClassService.updateStudent(teachingClassId, studentIds));
    }



    @PostMapping("/bulk/excelToTeachingClass")
    @ApiOperation("教学班导入")
    @LogAnnotation(operationType = "13", operationContent = "教学班信息导入")
    public void excelToTeachingClass(@ApiParam(value = "任务流程id", required = true) HttpServletResponse response,
                                     @ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                     @ApiParam(value = "1 追加 2 覆盖", required = true) @RequestParam Integer flag,
                                     @ApiParam("file") @RequestPart("file") MultipartFile file) {
        teachingClassService.excelToTeachingClass(response, taskId, flag, file);
    }





}
